{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.utils import np_utils\n",
    "from data import load_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using Theano backend.\n",
      "Using gpu device 0: GeForce GT 635M (CNMeM is disabled)\n",
      "D:\\Anaconda\\lib\\site-packages\\theano-0.8.0.dev0-py2.7.egg\\theano\\tensor\\signal\\downsample.py:5: UserWarning: downsample module has been moved to the pool module.\n",
      "  warnings.warn(\"downsample module has been moved to the pool module.\")\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import print_function\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "from keras.models import Sequential\n",
    "from keras.layers.core import Dense, Dropout, Activation, Flatten\n",
    "from keras.layers.advanced_activations import PReLU\n",
    "from keras.layers.convolutional import Convolution2D, MaxPooling2D\n",
    "from keras.optimizers import SGD, Adadelta, Adagrad\n",
    "from keras.utils import np_utils, generic_utils\n",
    "from six.moves import range\n",
    "from data import load_data\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data, label = load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.cross_validation import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data,label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "Y_train = np_utils.to_categorical(y_train,2)\n",
    "Y_test = np_utils.to_categorical(y_test,2)\n",
    "\n",
    "X_train = X_train.astype('float32')\n",
    "X_test = X_test.astype('float32')\n",
    "X_train /= 255\n",
    "X_test /=255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def make_network():\n",
    "    model = Sequential()\n",
    "    model.add(Convolution2D(32,3,3,border_mode='same',input_shape=(3,32,32)))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Convolution2D(32,3,3))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "    model.add(Dropout(0.25))\n",
    "\n",
    "    model.add(Convolution2D(64,3,3,border_mode='same'))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Convolution2D(64,3,3))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2,2)))\n",
    "    model.add(Dropout(0.25))\n",
    "\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(512))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(2))\n",
    "    model.add(Activation('softmax'))\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "nb_epoch = 200\n",
    "def train_model(model,X_train,Y_train,X_test,Y_test):\n",
    "    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
    "    model.compile(loss='categorical_crossentropy', optimizer=sgd)\n",
    "    model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size,\n",
    "             validation_split=0.1, show_accuracy=True, verbose=1)\n",
    "    print('Testing...')\n",
    "    res = model.evaluate(X_test, Y_test,\n",
    "                        batch_size=batch_size, verbose=1, show_accuracy=True)\n",
    "    print('Test accuracy: {0}'.format(res[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = make_network()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 13500 samples, validate on 1500 samples\n",
      "Epoch 1/200\n",
      "13500/13500 [==============================] - 104s - loss: 0.4326 - acc: 0.7988 - val_loss: 0.3267 - val_acc: 0.8667\n",
      "Epoch 2/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.2413 - acc: 0.9054 - val_loss: 0.2032 - val_acc: 0.9140\n",
      "Epoch 3/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.1706 - acc: 0.9330 - val_loss: 0.1172 - val_acc: 0.9547\n",
      "Epoch 4/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.1223 - acc: 0.9541 - val_loss: 0.1026 - val_acc: 0.9600\n",
      "Epoch 5/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.1128 - acc: 0.9576 - val_loss: 0.1227 - val_acc: 0.9527\n",
      "Epoch 6/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0980 - acc: 0.9627 - val_loss: 0.0769 - val_acc: 0.9740\n",
      "Epoch 7/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0830 - acc: 0.9691 - val_loss: 0.0733 - val_acc: 0.9733\n",
      "Epoch 8/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0766 - acc: 0.9725 - val_loss: 0.0728 - val_acc: 0.9733\n",
      "Epoch 9/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0743 - acc: 0.9739 - val_loss: 0.0679 - val_acc: 0.9720\n",
      "Epoch 10/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0610 - acc: 0.9791 - val_loss: 0.0616 - val_acc: 0.9760\n",
      "Epoch 11/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0594 - acc: 0.9788 - val_loss: 0.0618 - val_acc: 0.9760\n",
      "Epoch 12/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0603 - acc: 0.9780 - val_loss: 0.0780 - val_acc: 0.9713\n",
      "Epoch 13/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0556 - acc: 0.9794 - val_loss: 0.0668 - val_acc: 0.9753\n",
      "Epoch 14/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0511 - acc: 0.9819 - val_loss: 0.0713 - val_acc: 0.9747\n",
      "Epoch 15/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0541 - acc: 0.9812 - val_loss: 0.0678 - val_acc: 0.9760\n",
      "Epoch 16/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0472 - acc: 0.9824 - val_loss: 0.0636 - val_acc: 0.9800\n",
      "Epoch 17/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0414 - acc: 0.9844 - val_loss: 0.0750 - val_acc: 0.9753\n",
      "Epoch 18/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0415 - acc: 0.9839 - val_loss: 0.0703 - val_acc: 0.9753\n",
      "Epoch 19/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0371 - acc: 0.9859 - val_loss: 0.0603 - val_acc: 0.9800\n",
      "Epoch 20/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0372 - acc: 0.9870 - val_loss: 0.0718 - val_acc: 0.9780\n",
      "Epoch 21/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0348 - acc: 0.9884 - val_loss: 0.0719 - val_acc: 0.9793\n",
      "Epoch 22/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0367 - acc: 0.9860 - val_loss: 0.0701 - val_acc: 0.9800\n",
      "Epoch 23/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0303 - acc: 0.9875 - val_loss: 0.0750 - val_acc: 0.9787\n",
      "Epoch 24/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0344 - acc: 0.9871 - val_loss: 0.0798 - val_acc: 0.9733\n",
      "Epoch 25/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0343 - acc: 0.9874 - val_loss: 0.0640 - val_acc: 0.9793\n",
      "Epoch 26/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0262 - acc: 0.9904 - val_loss: 0.0903 - val_acc: 0.9753\n",
      "Epoch 27/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0278 - acc: 0.9899 - val_loss: 0.0718 - val_acc: 0.9800\n",
      "Epoch 28/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0243 - acc: 0.9907 - val_loss: 0.0665 - val_acc: 0.9813\n",
      "Epoch 29/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0243 - acc: 0.9914 - val_loss: 0.0828 - val_acc: 0.9707\n",
      "Epoch 30/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0234 - acc: 0.9910 - val_loss: 0.0688 - val_acc: 0.9800\n",
      "Epoch 31/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0214 - acc: 0.9933 - val_loss: 0.0722 - val_acc: 0.9767\n",
      "Epoch 32/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0241 - acc: 0.9917 - val_loss: 0.0814 - val_acc: 0.9753\n",
      "Epoch 33/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0200 - acc: 0.9930 - val_loss: 0.0701 - val_acc: 0.9780\n",
      "Epoch 34/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0212 - acc: 0.9924 - val_loss: 0.0920 - val_acc: 0.9807\n",
      "Epoch 35/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0185 - acc: 0.9940 - val_loss: 0.0776 - val_acc: 0.9813\n",
      "Epoch 36/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0227 - acc: 0.9930 - val_loss: 0.0686 - val_acc: 0.9793\n",
      "Epoch 37/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0174 - acc: 0.9933 - val_loss: 0.0871 - val_acc: 0.9747\n",
      "Epoch 38/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0173 - acc: 0.9941 - val_loss: 0.0765 - val_acc: 0.9767\n",
      "Epoch 39/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0177 - acc: 0.9938 - val_loss: 0.0689 - val_acc: 0.9787\n",
      "Epoch 40/200\n",
      "13500/13500 [==============================] - 140s - loss: 0.0137 - acc: 0.9950 - val_loss: 0.0936 - val_acc: 0.9747\n",
      "Epoch 41/200\n",
      "13500/13500 [==============================] - 132s - loss: 0.0161 - acc: 0.9942 - val_loss: 0.0756 - val_acc: 0.9780\n",
      "Epoch 42/200\n",
      "13500/13500 [==============================] - 133s - loss: 0.0176 - acc: 0.9938 - val_loss: 0.0787 - val_acc: 0.9800\n",
      "Epoch 43/200\n",
      "13500/13500 [==============================] - 139s - loss: 0.0155 - acc: 0.9939 - val_loss: 0.0806 - val_acc: 0.9787\n",
      "Epoch 44/200\n",
      "13500/13500 [==============================] - 138s - loss: 0.0134 - acc: 0.9952 - val_loss: 0.0790 - val_acc: 0.9820\n",
      "Epoch 45/200\n",
      "13500/13500 [==============================] - 133s - loss: 0.0150 - acc: 0.9939 - val_loss: 0.0765 - val_acc: 0.9767\n",
      "Epoch 46/200\n",
      "13500/13500 [==============================] - 132s - loss: 0.0126 - acc: 0.9953 - val_loss: 0.0717 - val_acc: 0.9833\n",
      "Epoch 47/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0129 - acc: 0.9956 - val_loss: 0.0812 - val_acc: 0.9767\n",
      "Epoch 48/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0107 - acc: 0.9967 - val_loss: 0.0919 - val_acc: 0.9793\n",
      "Epoch 49/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0161 - acc: 0.9941 - val_loss: 0.0894 - val_acc: 0.9767\n",
      "Epoch 50/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0184 - acc: 0.9933 - val_loss: 0.0696 - val_acc: 0.9780\n",
      "Epoch 51/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0114 - acc: 0.9964 - val_loss: 0.0927 - val_acc: 0.9780\n",
      "Epoch 52/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0123 - acc: 0.9961 - val_loss: 0.0790 - val_acc: 0.9767\n",
      "Epoch 53/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0102 - acc: 0.9964 - val_loss: 0.0826 - val_acc: 0.9807\n",
      "Epoch 54/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0108 - acc: 0.9962 - val_loss: 0.0893 - val_acc: 0.9800\n",
      "Epoch 55/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0095 - acc: 0.9974 - val_loss: 0.0898 - val_acc: 0.9753\n",
      "Epoch 56/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0113 - acc: 0.9959 - val_loss: 0.0795 - val_acc: 0.9820\n",
      "Epoch 57/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0111 - acc: 0.9961 - val_loss: 0.0837 - val_acc: 0.9793\n",
      "Epoch 58/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0136 - acc: 0.9957 - val_loss: 0.0929 - val_acc: 0.9820\n",
      "Epoch 59/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0112 - acc: 0.9965 - val_loss: 0.0701 - val_acc: 0.9847\n",
      "Epoch 60/200\n",
      "13500/13500 [==============================] - 109s - loss: 0.0108 - acc: 0.9969 - val_loss: 0.1234 - val_acc: 0.9747\n",
      "Epoch 61/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0121 - acc: 0.9959 - val_loss: 0.0890 - val_acc: 0.9753\n",
      "Epoch 62/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0092 - acc: 0.9965 - val_loss: 0.0892 - val_acc: 0.9807\n",
      "Epoch 63/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0100 - acc: 0.9967 - val_loss: 0.0924 - val_acc: 0.9780\n",
      "Epoch 64/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0092 - acc: 0.9973 - val_loss: 0.0808 - val_acc: 0.9827\n",
      "Epoch 65/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0114 - acc: 0.9956 - val_loss: 0.0751 - val_acc: 0.9840\n",
      "Epoch 66/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0053 - acc: 0.9984 - val_loss: 0.0845 - val_acc: 0.9807\n",
      "Epoch 67/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0105 - acc: 0.9964 - val_loss: 0.0791 - val_acc: 0.9820\n",
      "Epoch 68/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0073 - acc: 0.9977 - val_loss: 0.0728 - val_acc: 0.9800\n",
      "Epoch 69/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0082 - acc: 0.9977 - val_loss: 0.0877 - val_acc: 0.9793\n",
      "Epoch 70/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0089 - acc: 0.9970 - val_loss: 0.0873 - val_acc: 0.9773\n",
      "Epoch 71/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0075 - acc: 0.9978 - val_loss: 0.0951 - val_acc: 0.9793\n",
      "Epoch 72/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0080 - acc: 0.9973 - val_loss: 0.0909 - val_acc: 0.9767\n",
      "Epoch 73/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0066 - acc: 0.9975 - val_loss: 0.1018 - val_acc: 0.9807\n",
      "Epoch 74/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0084 - acc: 0.9971 - val_loss: 0.1014 - val_acc: 0.9767\n",
      "Epoch 75/200\n",
      "13500/13500 [==============================] - 109s - loss: 0.0155 - acc: 0.9949 - val_loss: 0.0969 - val_acc: 0.9727\n",
      "Epoch 76/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0063 - acc: 0.9978 - val_loss: 0.0766 - val_acc: 0.9813\n",
      "Epoch 77/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0073 - acc: 0.9972 - val_loss: 0.0844 - val_acc: 0.9787\n",
      "Epoch 78/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0075 - acc: 0.9976 - val_loss: 0.0954 - val_acc: 0.9787\n",
      "Epoch 79/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0106 - acc: 0.9964 - val_loss: 0.0894 - val_acc: 0.9793\n",
      "Epoch 80/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0068 - acc: 0.9974 - val_loss: 0.1056 - val_acc: 0.9800\n",
      "Epoch 81/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0073 - acc: 0.9974 - val_loss: 0.0928 - val_acc: 0.9840\n",
      "Epoch 82/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0051 - acc: 0.9981 - val_loss: 0.1282 - val_acc: 0.9767\n",
      "Epoch 83/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0134 - acc: 0.9952 - val_loss: 0.0809 - val_acc: 0.9820\n",
      "Epoch 84/200\n",
      "13500/13500 [==============================] - 108s - loss: 0.0081 - acc: 0.9979 - val_loss: 0.0876 - val_acc: 0.9767\n",
      "Epoch 85/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0070 - acc: 0.9973 - val_loss: 0.0838 - val_acc: 0.9793\n",
      "Epoch 86/200\n",
      "13500/13500 [==============================] - 114s - loss: 0.0065 - acc: 0.9978 - val_loss: 0.0936 - val_acc: 0.9807\n",
      "Epoch 87/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0089 - acc: 0.9978 - val_loss: 0.0845 - val_acc: 0.9827\n",
      "Epoch 88/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0064 - acc: 0.9979 - val_loss: 0.0790 - val_acc: 0.9847\n",
      "Epoch 89/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0050 - acc: 0.9981 - val_loss: 0.0838 - val_acc: 0.9820\n",
      "Epoch 90/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0069 - acc: 0.9979 - val_loss: 0.0757 - val_acc: 0.9820\n",
      "Epoch 91/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0122 - acc: 0.9961 - val_loss: 0.0781 - val_acc: 0.9780\n",
      "Epoch 92/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0060 - acc: 0.9984 - val_loss: 0.0887 - val_acc: 0.9760\n",
      "Epoch 93/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0076 - acc: 0.9973 - val_loss: 0.0923 - val_acc: 0.9793\n",
      "Epoch 94/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0099 - acc: 0.9970 - val_loss: 0.0760 - val_acc: 0.9813\n",
      "Epoch 95/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0085 - acc: 0.9969 - val_loss: 0.0874 - val_acc: 0.9807\n",
      "Epoch 96/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0098 - acc: 0.9971 - val_loss: 0.0838 - val_acc: 0.9807\n",
      "Epoch 97/200\n",
      "13500/13500 [==============================] - 115s - loss: 0.0049 - acc: 0.9984 - val_loss: 0.1152 - val_acc: 0.9753\n",
      "Epoch 98/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0051 - acc: 0.9981 - val_loss: 0.1022 - val_acc: 0.9767\n",
      "Epoch 99/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0033 - acc: 0.9988 - val_loss: 0.1015 - val_acc: 0.9793\n",
      "Epoch 100/200\n",
      "13500/13500 [==============================] - 109s - loss: 0.0086 - acc: 0.9967 - val_loss: 0.0903 - val_acc: 0.9813\n",
      "Epoch 101/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0062 - acc: 0.9981 - val_loss: 0.0848 - val_acc: 0.9820\n",
      "Epoch 102/200\n",
      "13500/13500 [==============================] - 107s - loss: 0.0030 - acc: 0.9990 - val_loss: 0.1025 - val_acc: 0.9820\n",
      "Epoch 103/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0122 - acc: 0.9965 - val_loss: 0.0876 - val_acc: 0.9773\n",
      "Epoch 104/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0065 - acc: 0.9984 - val_loss: 0.0980 - val_acc: 0.9767\n",
      "Epoch 105/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0047 - acc: 0.9986 - val_loss: 0.1161 - val_acc: 0.9787\n",
      "Epoch 106/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0066 - acc: 0.9981 - val_loss: 0.0918 - val_acc: 0.9793\n",
      "Epoch 107/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0022 - acc: 0.9992 - val_loss: 0.0977 - val_acc: 0.9793\n",
      "Epoch 108/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0040 - acc: 0.9985 - val_loss: 0.0976 - val_acc: 0.9787\n",
      "Epoch 109/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0053 - acc: 0.9982 - val_loss: 0.1056 - val_acc: 0.9773\n",
      "Epoch 110/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0088 - acc: 0.9971 - val_loss: 0.1044 - val_acc: 0.9813\n",
      "Epoch 111/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0042 - acc: 0.9987 - val_loss: 0.1282 - val_acc: 0.9767\n",
      "Epoch 112/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0050 - acc: 0.9983 - val_loss: 0.1037 - val_acc: 0.9753\n",
      "Epoch 113/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0034 - acc: 0.9985 - val_loss: 0.0983 - val_acc: 0.9793\n",
      "Epoch 114/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0042 - acc: 0.9987 - val_loss: 0.1054 - val_acc: 0.9807\n",
      "Epoch 115/200\n",
      "13500/13500 [==============================] - 105s - loss: 0.0083 - acc: 0.9974 - val_loss: 0.0949 - val_acc: 0.9767\n",
      "Epoch 116/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0060 - acc: 0.9980 - val_loss: 0.1053 - val_acc: 0.9800\n",
      "Epoch 117/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0046 - acc: 0.9984 - val_loss: 0.1045 - val_acc: 0.9773\n",
      "Epoch 118/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0063 - acc: 0.9981 - val_loss: 0.0946 - val_acc: 0.9787\n",
      "Epoch 119/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0049 - acc: 0.9986 - val_loss: 0.0853 - val_acc: 0.9813\n",
      "Epoch 120/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0084 - acc: 0.9971 - val_loss: 0.1232 - val_acc: 0.9760\n",
      "Epoch 121/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0070 - acc: 0.9973 - val_loss: 0.1246 - val_acc: 0.9747\n",
      "Epoch 122/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0083 - acc: 0.9974 - val_loss: 0.0698 - val_acc: 0.9827\n",
      "Epoch 123/200\n",
      "13500/13500 [==============================] - 106s - loss: 0.0047 - acc: 0.9986 - val_loss: 0.1123 - val_acc: 0.9800\n",
      "Epoch 124/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0067 - acc: 0.9979 - val_loss: 0.0796 - val_acc: 0.9800\n",
      "Epoch 125/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0077 - acc: 0.9973 - val_loss: 0.0939 - val_acc: 0.9813\n",
      "Epoch 126/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0033 - acc: 0.9989 - val_loss: 0.0956 - val_acc: 0.9773\n",
      "Epoch 127/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0027 - acc: 0.9992 - val_loss: 0.0843 - val_acc: 0.9820\n",
      "Epoch 128/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0023 - acc: 0.9993 - val_loss: 0.0938 - val_acc: 0.9807\n",
      "Epoch 129/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0012 - acc: 0.9996 - val_loss: 0.1031 - val_acc: 0.9807\n",
      "Epoch 130/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0040 - acc: 0.9988 - val_loss: 0.0939 - val_acc: 0.9847\n",
      "Epoch 131/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0024 - acc: 0.9990 - val_loss: 0.1065 - val_acc: 0.9807\n",
      "Epoch 132/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0042 - acc: 0.9990 - val_loss: 0.0844 - val_acc: 0.9853\n",
      "Epoch 133/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0082 - acc: 0.9973 - val_loss: 0.0852 - val_acc: 0.9800\n",
      "Epoch 134/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0041 - acc: 0.9987 - val_loss: 0.0862 - val_acc: 0.9820\n",
      "Epoch 135/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0043 - acc: 0.9984 - val_loss: 0.0897 - val_acc: 0.9813\n",
      "Epoch 136/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0018 - acc: 0.9993 - val_loss: 0.0937 - val_acc: 0.9840\n",
      "Epoch 137/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0029 - acc: 0.9991 - val_loss: 0.0933 - val_acc: 0.9813\n",
      "Epoch 138/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0029 - acc: 0.9990 - val_loss: 0.1021 - val_acc: 0.9800\n",
      "Epoch 139/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0015 - acc: 0.9994 - val_loss: 0.0887 - val_acc: 0.9820\n",
      "Epoch 140/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0034 - acc: 0.9988 - val_loss: 0.1619 - val_acc: 0.9700\n",
      "Epoch 141/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0059 - acc: 0.9981 - val_loss: 0.0878 - val_acc: 0.9827\n",
      "Epoch 142/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0037 - acc: 0.9990 - val_loss: 0.0886 - val_acc: 0.9813\n",
      "Epoch 143/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0070 - acc: 0.9976 - val_loss: 0.1008 - val_acc: 0.9800\n",
      "Epoch 144/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0039 - acc: 0.9986 - val_loss: 0.0973 - val_acc: 0.9820\n",
      "Epoch 145/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0055 - acc: 0.9982 - val_loss: 0.1077 - val_acc: 0.9767\n",
      "Epoch 146/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0018 - acc: 0.9996 - val_loss: 0.0962 - val_acc: 0.9787\n",
      "Epoch 147/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0041 - acc: 0.9987 - val_loss: 0.0942 - val_acc: 0.9793\n",
      "Epoch 148/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0018 - acc: 0.9992 - val_loss: 0.0959 - val_acc: 0.9840\n",
      "Epoch 149/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0027 - acc: 0.9990 - val_loss: 0.1154 - val_acc: 0.9807\n",
      "Epoch 150/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0061 - acc: 0.9980 - val_loss: 0.0897 - val_acc: 0.9813\n",
      "Epoch 151/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0041 - acc: 0.9984 - val_loss: 0.1134 - val_acc: 0.9780\n",
      "Epoch 152/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0024 - acc: 0.9993 - val_loss: 0.0922 - val_acc: 0.9820\n",
      "Epoch 153/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0020 - acc: 0.9994 - val_loss: 0.1051 - val_acc: 0.9787\n",
      "Epoch 154/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0015 - acc: 0.9995 - val_loss: 0.1142 - val_acc: 0.9773\n",
      "Epoch 155/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0016 - acc: 0.9996 - val_loss: 0.1080 - val_acc: 0.9807\n",
      "Epoch 156/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0011 - acc: 0.9996 - val_loss: 0.1058 - val_acc: 0.9807\n",
      "Epoch 157/200\n",
      "13500/13500 [==============================] - 111s - loss: 4.9034e-04 - acc: 0.9999 - val_loss: 0.1200 - val_acc: 0.9800\n",
      "Epoch 158/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0030 - acc: 0.9992 - val_loss: 0.1347 - val_acc: 0.9773\n",
      "Epoch 159/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0064 - acc: 0.9983 - val_loss: 0.1114 - val_acc: 0.9793\n",
      "Epoch 160/200\n",
      "13500/13500 [==============================] - 113s - loss: 0.0016 - acc: 0.9993 - val_loss: 0.1311 - val_acc: 0.9793\n",
      "Epoch 161/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0030 - acc: 0.9987 - val_loss: 0.1252 - val_acc: 0.9773\n",
      "Epoch 162/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0057 - acc: 0.9984 - val_loss: 0.1114 - val_acc: 0.9793\n",
      "Epoch 163/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0023 - acc: 0.9992 - val_loss: 0.1061 - val_acc: 0.9847\n",
      "Epoch 164/200\n",
      "13500/13500 [==============================] - 112s - loss: 0.0023 - acc: 0.9990 - val_loss: 0.1238 - val_acc: 0.9793\n",
      "Epoch 165/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0023 - acc: 0.9990 - val_loss: 0.1046 - val_acc: 0.9820\n",
      "Epoch 166/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0033 - acc: 0.9990 - val_loss: 0.1047 - val_acc: 0.9807\n",
      "Epoch 167/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0033 - acc: 0.9989 - val_loss: 0.1087 - val_acc: 0.9800\n",
      "Epoch 168/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0011 - acc: 0.9996 - val_loss: 0.1133 - val_acc: 0.9813\n",
      "Epoch 169/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0075 - acc: 0.9979 - val_loss: 0.0935 - val_acc: 0.9840\n",
      "Epoch 170/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0028 - acc: 0.9991 - val_loss: 0.0918 - val_acc: 0.9813\n",
      "Epoch 171/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0037 - acc: 0.9987 - val_loss: 0.1015 - val_acc: 0.9820\n",
      "Epoch 172/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0072 - acc: 0.9979 - val_loss: 0.0950 - val_acc: 0.9800\n",
      "Epoch 173/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0094 - acc: 0.9973 - val_loss: 0.0914 - val_acc: 0.9853\n",
      "Epoch 174/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0066 - acc: 0.9979 - val_loss: 0.0984 - val_acc: 0.9793\n",
      "Epoch 175/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0036 - acc: 0.9990 - val_loss: 0.0907 - val_acc: 0.9787\n",
      "Epoch 176/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0020 - acc: 0.9993 - val_loss: 0.0915 - val_acc: 0.9827\n",
      "Epoch 177/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0029 - acc: 0.9990 - val_loss: 0.1003 - val_acc: 0.9813\n",
      "Epoch 178/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0031 - acc: 0.9987 - val_loss: 0.0979 - val_acc: 0.9827\n",
      "Epoch 179/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0032 - acc: 0.9989 - val_loss: 0.0976 - val_acc: 0.9820\n",
      "Epoch 180/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0020 - acc: 0.9995 - val_loss: 0.0901 - val_acc: 0.9827\n",
      "Epoch 181/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0041 - acc: 0.9983 - val_loss: 0.0922 - val_acc: 0.9833\n",
      "Epoch 182/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0044 - acc: 0.9987 - val_loss: 0.1134 - val_acc: 0.9733\n",
      "Epoch 183/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0032 - acc: 0.9988 - val_loss: 0.0910 - val_acc: 0.9793\n",
      "Epoch 184/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0018 - acc: 0.9994 - val_loss: 0.0912 - val_acc: 0.9807\n",
      "Epoch 185/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0020 - acc: 0.9991 - val_loss: 0.1228 - val_acc: 0.9787\n",
      "Epoch 186/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0051 - acc: 0.9982 - val_loss: 0.1056 - val_acc: 0.9800\n",
      "Epoch 187/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0060 - acc: 0.9985 - val_loss: 0.0852 - val_acc: 0.9807\n",
      "Epoch 188/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0041 - acc: 0.9987 - val_loss: 0.1102 - val_acc: 0.9747\n",
      "Epoch 189/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0031 - acc: 0.9987 - val_loss: 0.0788 - val_acc: 0.9820\n",
      "Epoch 190/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0022 - acc: 0.9992 - val_loss: 0.0891 - val_acc: 0.9807\n",
      "Epoch 191/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0029 - acc: 0.9991 - val_loss: 0.0885 - val_acc: 0.9820\n",
      "Epoch 192/200\n",
      "13500/13500 [==============================] - 110s - loss: 5.3104e-04 - acc: 0.9999 - val_loss: 0.0906 - val_acc: 0.9820\n",
      "Epoch 193/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0017 - acc: 0.9995 - val_loss: 0.1200 - val_acc: 0.9760\n",
      "Epoch 194/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0038 - acc: 0.9987 - val_loss: 0.1117 - val_acc: 0.9807\n",
      "Epoch 195/200\n",
      "13500/13500 [==============================] - 111s - loss: 0.0023 - acc: 0.9994 - val_loss: 0.1178 - val_acc: 0.9807\n",
      "Epoch 196/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0036 - acc: 0.9989 - val_loss: 0.1116 - val_acc: 0.9767\n",
      "Epoch 197/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0029 - acc: 0.9990 - val_loss: 0.1128 - val_acc: 0.9800\n",
      "Epoch 198/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0013 - acc: 0.9996 - val_loss: 0.1012 - val_acc: 0.9847\n",
      "Epoch 199/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0048 - acc: 0.9983 - val_loss: 0.1366 - val_acc: 0.9767\n",
      "Epoch 200/200\n",
      "13500/13500 [==============================] - 110s - loss: 0.0047 - acc: 0.9985 - val_loss: 0.1072 - val_acc: 0.9807\n",
      "Testing...\n",
      "5000/5000 [==============================] - 12s    \n",
      "Test accuracy: 0.9844\n"
     ]
    }
   ],
   "source": [
    "train_model(model,X_train,Y_train,X_test,Y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model_json = model.to_json()\n",
    "open('jianghuang_articture.json', 'w').write(model_json)\n",
    "model.save_weights('jianhuang_weights.h5', overwrite=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.misc\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_and_scale_imgs():\n",
    "   img_names = os.listdir('imgs_p/')\n",
    "\n",
    "   imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread('imgs_p/'+img_name), (32, 32)),\n",
    "                        (2, 0, 1)).astype('float32')\n",
    "           for img_name in img_names]\n",
    "   return np.array(imgs) / 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "imgs = load_and_scale_imgs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 21s    \n"
     ]
    }
   ],
   "source": [
    "predictions = model.predict_classes(imgs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, ..., 1, 1, 1], dtype=int64)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def predictImage(url):\n",
    "    img_names = [url,]\n",
    "    imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread(img_name), (32, 32)),\n",
    "                        (2, 0, 1)).astype('float32')\n",
    "           for img_name in img_names]\n",
    "    imgs = np.array(imgs) / 255\n",
    "    predictions = model.predict_classes(imgs)\n",
    "    return predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1], dtype=int64)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictImage('imgs_p/whgnhb1star003pl111.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0], dtype=int64)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictImage('imgs_n/100065_01111.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
